﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GestionStock
{
    public partial class GestionStockLogin : Form
    {
        public GestionStockLogin()
        {
            InitializeComponent();
        }

        private bool ValidateTextBoxes()
        {

            if (txtUserName.Text.Trim().Length == 0)
            {

                errorProvider1.SetError(txtUserName, "Veuillez saisie un nom d'utilisateur valid");
                return false;
            }
            else
            {
                errorProvider1.SetError(txtUserName, "");
            }
            if (txtPassword.Text.Trim().Length == 0)
            {
                errorProvider1.SetError(txtPassword, "Veuillez saisir un mot de passe");
                return false;
            }
            else
            {
                errorProvider1.SetError(txtPassword, "");
            }
            return true;
        }


        private void btnOk_Click(object sender, EventArgs e)
        {
            if (ValidateTextBoxes())
            {

                SqlConnection oConn = new SqlConnection();
                oConn.ConnectionString = Properties.Settings.Default.connexionString;
                oConn.Open();
                string strQuery = "select loginName from tblUser where loginName='" + txtUserName.Text + "' and password='" + txtPassword.Text + "'";
                object retVal;
                SqlCommand cmd = new SqlCommand(strQuery, oConn);
                retVal = cmd.ExecuteScalar();
                if (retVal != null)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.DialogResult = DialogResult.Cancel;
                    errorProvider1.SetError(btnOk, "Login ou mot de passe invalid, contacter l'administrateur !!");
                }

            }

        }

        private void GestionStockLogin_Load(object sender, EventArgs e)
        {
            this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
                          (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
        }

    }
}
